#include "Material.h"
#include "resource.h"

Material::Material(char* name, char* diffuseTexture, char* normalTexture, char* specularTexture)
{
	pDiffuseTexture = NULL;
    pNormalTexture = NULL;
    pSpecularTexture = NULL;
	pDiffuseView = NULL;
	pNormalView = NULL;
	pSpecularView = NULL;

	strcpy_s(this->name, MAX_MATERIAL_NAME, name);
	if (strlen(diffuseTexture) == 0)
		this->diffuseTexture[0] = 0;
	else
		strcpy_s(this->diffuseTexture, MAX_TEXTURE_NAME, diffuseTexture);
	if (strlen(normalTexture) == 0)
		this->normalTexture[0] = 0;
	else
		strcpy_s(this->normalTexture, MAX_TEXTURE_NAME, normalTexture);
	if (strlen(specularTexture) == 0)
		this->specularTexture[0] = 0;
	else
		strcpy_s(this->specularTexture, MAX_TEXTURE_NAME, specularTexture);
}

void Material::UnloadGraphicsContent()
{
	if (pSpecularView != NULL) pSpecularView->Release();
	pSpecularView = NULL;
	if (pSpecularTexture != NULL) pSpecularTexture->Release();
	pSpecularTexture = NULL;
	if (pNormalView != NULL) pNormalView->Release();
	pNormalView = NULL;
	if (pNormalTexture != NULL) pNormalTexture->Release();
	pNormalTexture = NULL;
	if (pDiffuseView != NULL) pDiffuseView->Release();
	pDiffuseView = NULL;
	if (pDiffuseTexture != NULL) pDiffuseTexture->Release();
	pDiffuseTexture = NULL;
}

HRESULT Material::LoadGraphicsContent(EnginePointer<ID3D10Device>& pDevice)
{
	HRESULT hr;

	HMODULE module = GetModuleHandle(L"D3D10enginecontent.dll");
	LPWSTR rsStr = MAKEINTRESOURCE(DEFAULT_DIFFUSE);

	D3DX10_IMAGE_LOAD_INFO loadInfo;
	D3DX10_IMAGE_INFO srcInfo;
	if (diffuseTexture[0] == 0)
		hr = D3DX10GetImageInfoFromResource(module, rsStr, NULL,& srcInfo, NULL);
	else
		hr = D3DX10GetImageInfoFromFileA(diffuseTexture, NULL,& srcInfo, NULL);
	if (FAILED(hr)) return hr;
	loadInfo.pSrcInfo =& srcInfo;
	loadInfo.Format = loadInfo.pSrcInfo->Format;

	D3D10_SHADER_RESOURCE_VIEW_DESC viewDesc;
	viewDesc.ViewDimension = D3D10_SRV_DIMENSION_TEXTURE2D;
	viewDesc.Texture2D.MostDetailedMip = 0;

	if (diffuseTexture[0] == 0)
		hr = D3DX10CreateTextureFromResource(pDevice, module, rsStr,& loadInfo, NULL, (ID3D10Resource**)&pDiffuseTexture, NULL);
	else
		hr = D3DX10CreateTextureFromFileA(pDevice, diffuseTexture,& loadInfo, NULL, (ID3D10Resource**)&pDiffuseTexture, NULL);
	if (FAILED(hr)) return hr;

	viewDesc.Format = loadInfo.pSrcInfo->Format;
	viewDesc.Texture2D.MipLevels = loadInfo.pSrcInfo->MipLevels;
	hr = pDevice->CreateShaderResourceView(pDiffuseTexture,& viewDesc,& pDiffuseView);
	if (FAILED(hr)) return hr;

	rsStr = MAKEINTRESOURCE(DEFAULT_NORMAL);
	if (normalTexture[0] == 0)
		hr = D3DX10GetImageInfoFromResource(module, rsStr, NULL,& srcInfo, NULL);
	else
		hr = D3DX10GetImageInfoFromFileA(normalTexture, NULL,& srcInfo, NULL);
	if (FAILED(hr)) return hr;
	loadInfo.pSrcInfo =& srcInfo;
	loadInfo.Format = loadInfo.pSrcInfo->Format;

	if (normalTexture[0] == 0)
		hr = D3DX10CreateTextureFromResource(pDevice, module, rsStr,& loadInfo, NULL, (ID3D10Resource**)&pNormalTexture, NULL);
	else
		hr = D3DX10CreateTextureFromFileA(pDevice, normalTexture,& loadInfo, NULL, (ID3D10Resource**)&pNormalTexture, NULL);
	if (FAILED(hr)) return hr;
	viewDesc.Format = loadInfo.pSrcInfo->Format;
	viewDesc.Texture2D.MipLevels = loadInfo.pSrcInfo->MipLevels;
	hr = pDevice->CreateShaderResourceView(pNormalTexture,& viewDesc,& pNormalView);
	if (FAILED(hr)) return hr;

	rsStr = MAKEINTRESOURCE(DEFAULT_SPECULAR);
	if (specularTexture[0] == 0)
		hr = D3DX10GetImageInfoFromResource(module, rsStr, NULL,& srcInfo, NULL);
	else
		hr = D3DX10GetImageInfoFromFileA(specularTexture, NULL,& srcInfo, NULL);
	if (FAILED(hr)) return hr;
	loadInfo.pSrcInfo =& srcInfo;
	loadInfo.Format = loadInfo.pSrcInfo->Format;

	if (specularTexture[0] == 0)
		hr = D3DX10CreateTextureFromResource(pDevice, module, rsStr,& loadInfo, NULL, (ID3D10Resource**)&pSpecularTexture, NULL);
	else
		hr = D3DX10CreateTextureFromFileA(pDevice, specularTexture,& loadInfo, NULL, (ID3D10Resource**)&pSpecularTexture, NULL);
	if (FAILED(hr)) return hr;

	viewDesc.Format = loadInfo.pSrcInfo->Format;
	viewDesc.Texture2D.MipLevels = loadInfo.pSrcInfo->MipLevels;
	hr = pDevice->CreateShaderResourceView(pSpecularTexture,& viewDesc,& pSpecularView);
	if (FAILED(hr)) return hr;

	return S_OK;
}

ID3D10ShaderResourceView* Material::getDiffuseView()
{
	return pDiffuseView;
}

ID3D10ShaderResourceView* Material::getNormalView()
{
	return pNormalView;
}

ID3D10ShaderResourceView* Material::getSpecularView()
{
	return pSpecularView;
}
